package com.itic.web.mem.member.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;  

/** 
 * jackson 复杂 对象集合 的几种简单转换 
 * @author lenovo 
 * 
 * @param <T> 
 */  
public class Main<T>  
{  
    static ObjectMapper mapper = new ObjectMapper();  
    /*** 
     * 转对象 
     * @param josn 
     * @param clz 
     * @return 
     */  
    public T jsonStreamConverObject(String josn, Class<T> clz)  
    {  
  
        T t = null;  
        // ObjectMapper jacksonMapper = new ObjectMapper();  
        InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(  
                josn.getBytes()));  
        BufferedReader streamReader = new BufferedReader(in);  
        StringBuilder buff = new StringBuilder();  
        String inputStr;  
        try  
        {  
            while ((inputStr = streamReader.readLine()) != null)  
                buff.append(inputStr);  
            // ObjectMapper mapper = new ObjectMapper();  
            t = mapper.readValue(buff.toString(), clz);  
  
        } catch (IOException e)  
        {  
  
            e.printStackTrace();  
        }  
  
        return t;  
    }  
  
    /*** 
     * 转对象 
     * @param josn 
     * @param clz 
     * @return 
     */  
    public T jsonConverObject(String josn, Class<T> clz)  
    {  
  
        T t = null;  
        try  
        {  
            t = mapper.readValue(josn, clz);  
        } catch (JsonParseException e)  
        {  
            e.printStackTrace();  
        } catch (JsonMappingException e)  
        {  
            e.printStackTrace();  
        } catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
  
        return t;  
    }  
  
    /** 
     * 转集合 
     * @param josn 
     * @param clz 
     * @return 
     */  
    public List<T> jsonConverList(String josn, Class<T> clz)  
    {  
  
        List<T> me = null;  
        try  
        {  
            // jacksonMapper  
            // .disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);  
            // jacksonMapper.enableDefaultTyping();  
            // jacksonMapper.setVisibility(JsonMethod.FIELD,JsonAutoDetect.Visibility.ANY);  
            // jacksonMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT,  
            // false);//格式化  
            // jacksonMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);  
            // jacksonMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,  
            // false);  
  
            JavaType javaType = mapper.getTypeFactory()  
                    .constructParametricType(List.class, clz);// clz.selGenType().getClass()  
  
            me = mapper.readValue(josn, javaType);  
        } catch (JsonParseException e)  
        {  
            e.printStackTrace();  
        } catch (JsonMappingException e)  
        {  
            e.printStackTrace();  
        } catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
  
        return me;  
    }  
}  

